package com.wq.httpclientcore.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wq.httpclientcore.convert.Convertor;
import com.wq.httpclientcore.entities.po.ApiConfig;
import com.wq.httpclientcore.entities.req.save.ApiConfigSaveREQ;
import com.wq.httpclientcore.entities.req.query.ApiConfigREQ;
import com.wq.httpclientcore.entities.vo.ApiConfigVO;
import com.wq.httpclientcore.model.Result;
import com.wq.httpclientcore.service.ApiConfigService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * @author wangqi
 * @since 2025-03-10 15:41:09
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/v1/module/apiConfig")
@Tag(name = "API 配置管理", description = "API 配置相关的操作")
public class ApiConfigController {

    private final ApiConfigService apiConfigService;

    @GetMapping("/list")
    @Parameters({@Parameter(name = "pageNo", hidden = true), @Parameter(name = "pageSize", hidden = true)})
    @Operation(summary = "获取 API 配置列表", description = "根据条件获取 API 配置列表")
    public Result<List<ApiConfigVO>> list(@ParameterObject ApiConfigREQ req) {
        List<ApiConfig> poList = apiConfigService.list(req);
        if (poList == null || poList.isEmpty()) {
            return Result.success(Collections.emptyList());
        }
        List<ApiConfigVO> voList = Convertor.INSTANCE.convertApiConfigList(poList);
        return Result.success(voList);
    }

    @GetMapping("/page")
    @Operation(summary = "分页获取 API 配置列表", description = "根据条件分页获取 API 配置列表")
    public Result<IPage<ApiConfigVO>> page(ApiConfigREQ req) {
        IPage<ApiConfig> page = apiConfigService.pageList(req);
        IPage<ApiConfigVO> voPage = Convertor.INSTANCE.convertApiConfigPage(page);
        return Result.success(voPage);
    }

    @PostMapping
    @Operation(summary = "新增 API 配置", description = "新增一条 API 配置信息")
    public Result<String> add(@RequestBody ApiConfigSaveREQ req) {
        ApiConfig po = Convertor.INSTANCE.convert(req);
        boolean save = apiConfigService.saveApiConfig(po);
        if (!save) {
            return Result.fail("新增失败");
        }
        return Result.success("新增成功");
    }

    @PutMapping("/{id}")
    @Operation(summary = "编辑 API 配置", description = "根据 ID 编辑一条 API 配置信息")
    public Result<String> edit(@PathVariable("id") Long id, @RequestBody ApiConfigSaveREQ req) {
        ApiConfig po = Convertor.INSTANCE.convert(req);
        po.setId(id);
        boolean update = apiConfigService.updateApiConfig(po);
        if (!update) {
            return Result.fail("修改失败");
        }
        return Result.success("修改成功");
    }

    @DeleteMapping("/{ids}")
    @Operation(summary = "删除 API 配置", description = "根据 ID 数组删除多条 API 配置信息")
    public Result<String> delete(@PathVariable Long[] ids) {
        apiConfigService.removeApiConfigs(Arrays.asList(ids));
        return Result.success("删除成功");
    }
}